By default, all C# ASP.NET web applications created with Visual Studio 2010 are automatically provided with a Web.config file. However, if you ever need to manually insert a Web.config file into your site (e.g., when you are working with the single-page model and have not created a web solution), you may do so using the using the Website > Add New Item menu option. In either case, within this scope of a Web.config file you are able to add settings that control how your web application will function at runtime.
Recall during your examination of .NET assemblies (in Chapter 15) that you learned client applications can leverage an XML-based configuration file to instruct the CLR how it should handle binding requests, assembly probing, and other runtime details. The same holds true for ASP.NET web applications, with the notable exception that web-centric configuration files are always named Web.config (unlike *.exe configuration files, which are named based on the related client executable).
The full structure of a Web.config file is rather verbose. However, Table 32-9 outlines some of the more interesting subelements that can be found within a Web.config file.
Table 32-9. Select Elements of a Web.config File
Element | Meaning in Life |
---|---|
<appSettings> | This element is used to establish custom name/value pairs that can be programmatically read in memory for use by your pages using the ConfigurationManager type. |
<authentication> | This security-related element is used to define the authentication mode for this web application. |
<authorization> | This is another security-centric element used to define which users can access which resources on the web server. |
<connectionStrings> | This element is used to hold external connection strings used within this website. |
<customErrors> | This element is used to tell the runtime exactly how to display errors that occur during the functioning of the web application. |
<globalization> | This element is used to configure the globalization settings for this web application. |
<namespaces> | This element documents all of the namespaces to include if your web application has been precompiled using the new aspnet_compiler.exe command-line tool. |
<sessionState> | This element is used to control how and where session state data will be stored by the .NET runtime. |
<trace> | This element is used to enable (or disable) tracing support for this web application. |
A Web.config file may contain additional subelements above and beyond the set presented in Table 32-9. The vast majority of these items are security related, while the remaining items are useful only during advanced ASP.NET scenarios such as creating with custom HTTP headers or custom HTTP modules (topics that are not covered here).
Although you are always free to modify the content of a Web.config file directly using Visual Studio 2010, ASP.NET web projects can make use of a handy web-based editor that will allow you to graphically edit numerous elements and attributes of your project’s Web.config file. To launch this tool, activate the Website > ASP.NET Configuration menu option.
If you were to click the tabs located on the top of the page, you would quickly notice that most of this tool’s functionality is used to establish security settings for your website. However, this tool also makes it possible to add settings to your <appSettings> element, define debugging and tracing settings, and establish a default error page.
You’ll see more of this tool in action where necessary; however, do be aware that this utility will not allow you to add all possible settings to a Web.config file. There will most certainly be times when you will need to manually update this file using your text editor of choice.